<?php

namespace Biz\model;

/**
 *
 * @author lee
 *
 */

use Doraemon\tools\Log as tLog;
use Biz\validate\ShopContactInfo as vShopContactInfo;


class ShopContactInfo extends \Doraemon\pockets\datebase\BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop_info';

    const _DATABASE_ = 'shop_info';

    const _TABLE_NAME = 'shop_contact_info';


    const IS_MASTER_YES = 1;
    const IS_MASTER_NO = 0;


    //状态
    const STATUS_CANUSE = 1;//可用
    const STATUS_DELETE = 9;// 软删除


    public static function createOrEdit($uid, $data, $scenes = null, $method = null)
    {
        try {
            $result = [];

            $where = ['uid' => $uid];

            if (isset($data['master'])) {
                $where['master'] = self::IS_MASTER_YES;
            }

            if (isset($data['id'])) {
                $where['id'] = $data['id'];
            }

            if (is_null($method)) {

                if (isset($where['master']) || isset($where['id'])) {
                    $has = self::has_master($where);
                }

                $method = (isset($has) && $has) ? 'update' : 'insert';
            }


            if (!is_null($scenes)) {
                //提交字段进行校验
                if (!vShopContactInfo::validate($data, $scenes)) {

                    $msg = vShopContactInfo::firstError();

                    throw new \Exception($msg);
                }
            }


            switch ($method) {
                case 'insert':
                    $data['shop_id'] = $uid;
                    $data['uid'] = $uid;
                    $data['addtime'] = date('Y-m-d H:i:s');

                    $ret = self::insert($data);

                    $result['last_id'] = self::id();
                    break;
                case 'update':
                    $ret = self::update($data, $where);

                    $result[$method] = $ret->rowCount();
                    break;
            }

            if ($ret->errorCode() !== '00000') {
                throw new \Exception('数据库操作错误');
            }

            return $result;


        } catch (\Exception $e) {


            tLog::warning("保存联系人资料失败", ['uid' => $uid, 'data' => $data, 'e_msg' => $e->getMessage()]);


            throw $e;
        }
    }


    public static function delById($shopId,$id)
    {
        try {
            $ret = self::update(['status'=>self::STATUS_DELETE],['id' => $id,'shop_id'=>$shopId, 'master' => self::IS_MASTER_NO]);


            if ($ret->rowCount() == 0) {
                throw new \Exception('联系人不存在或删除错误');
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }

    public static function forceDelById($id)
    {
        try {
            $ret = self::update(['status'=>self::STATUS_DELETE],['id' => $id]);


            if ($ret->rowCount() == 0) {
                throw new \Exception('联系人不存在或删除错误');
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }

    public static function getMasterInfo($uid)
    {
        return self::get('*', ['shop_id' => $uid, 'master' => self::IS_MASTER_YES,'status'=>self::STATUS_CANUSE]);
    }

    public static function getAllContactInfo($uid)
    {
        return self::select('*', ['shop_id' => $uid,'status'=>self::STATUS_CANUSE, 'ORDER' => ['master' => 'DESC']]);
    }

    //获取商家联系人中的发货专员的信息
    public static function getShippingSpecialistInfos($shopId)
    {
        return self::select('*', ['shop_id' => $shopId,'status'=>self::STATUS_CANUSE, 'position' => '发货专员']);
    }

    //获取商家联系人中的退款专员的信息
    public static function getRefundCommissionerInfos($shopId)
    {
        return self::select('*', ['shop_id' => $shopId,'status'=>self::STATUS_CANUSE, 'position' => '退款专员']);
    }


    public static function setMaster($id)
    {
        try {
            $ret = self::update(['master' => self::IS_MASTER_YES], ['id' => $id]);

            if ($ret->rowCount() == 0) {
                throw new \Exception('更新主联系失败');
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }

    public static function getList($uid)
    {
        $list = self::select("*", ['uid' => $uid,'status'=>self::STATUS_CANUSE]);


        return $list;
    }

}

